/**
 * Created by MADinow on 25.10.2014.
 */

/* -----------------------------------------------------------
            GENERATOR FOR HTML ELEMENTS
 ----------------------------------------------------------- */

define(['jquery'], function(jQuery){

    /**
     * assigns a child HTML element to a parent HTML element
     * @param child
     * @param parent
     * @param appendParam
     * @private
     */
    function _assignElementToParent(child, parent, appendParam){
        if(child && parent){
            if(appendParam){
                parent.append(child);
            }else{
                parent.prepend(child);
            }
        }
    }

    /**
     * adds id, class and title to specified HTML element
     * @param targetElement
     * @param targetID
     * @param targetClass
     * @param targetTitle
     * @private
     */
    function _assignBasicAttributes(targetElement, targetID, targetClass, targetTitle){
        if(targetElement){
            if(targetID)
                targetElement.attr('id', targetID);
            if(targetClass)
                targetElement.addClass(targetClass);
            if(targetTitle)
                targetElement.attr('title', targetTitle);
        }
    }

    /**
     * creates a new header link element ( e.g. for CSS or FAVICON )
     * @param targetType
     * @param targetHref
     * @param targetRel
     * @returns created header link element
     * @private
     */
    function _createHeaderLink(targetType, targetHref, targetRel){
        var headerLink = jQuery(document.createElement('link'));
        if(targetType)
            headerLink.attr('type', targetType);
        if(targetHref)
            headerLink.attr('href', targetHref);
        if(targetRel)
            headerLink.attr('rel', targetRel);
        _assignElementToParent(headerLink, $("head"), true);
        return headerLink;
    }

    /**
     * sets the title of the page
     * @param targetTitle
     * @private
     */
    function _createPageTitle(targetTitle){
        var title = jQuery(document.createElement('title'));
        if(targetTitle)
            title.text(targetTitle);
        _assignElementToParent(title, $("head"), true);
    }

    /**
     * creates a new div element
     * @param targetParent
     * @param targetID
     * @param targetClass
     * @param appendParam
     * @returns created div element
     * @private
     */
    function _createDiv(targetParent, targetID, targetClass, appendParam){
        var div = jQuery(document.createElement('div'));
        _assignBasicAttributes(div, targetID, targetClass, null);
        _assignElementToParent(div, targetParent, appendParam);
        return div ;
    }

    /**
     * creates a new paragraph element
     * @param targetParent
     * @param targetID
     * @param targetClass
     * @param targetTitle
     * @param targetText
     * @param appendParam
     * @returns created paragraph element
     * @private
     */
    function _createParagraph(targetParent, targetID, targetClass, targetTitle, targetText, appendParam){
        var p = jQuery(document.createElement('p'));
        _assignBasicAttributes(p, targetID, targetClass, targetTitle);
        if(targetText)
            p.text(targetText);
        _assignElementToParent(p, targetParent, appendParam);
        return p ;
    }

    /**
     * creates new span element
     * @param targetParent
     * @param targetID
     * @param targetClass
     * @param targetTitle
     * @param targetText
     * @param appendParam
     * @returns created span element
     * @private
     */
    function _createSpan(targetParent, targetID, targetClass, targetTitle, targetText, appendParam){
        var span = jQuery(document.createElement('span'));
        _assignBasicAttributes(span, targetID, targetClass, targetTitle);
        if(targetText)
            span.text(targetText);
        _assignElementToParent(span, targetParent, appendParam);
        return span ;
    }

    /**
     * creates new table element
     * @param targetParent
     * @param targetID
     * @param targetClass
     * @param appendParam
     * @returns created table element
     * @private
     */
    function _createTable(targetParent, targetID, targetClass, appendParam){
        var table = jQuery(document.createElement('table'));
        _assignBasicAttributes(table, targetID, targetClass, null);
        _assignElementToParent(table, targetParent, appendParam);
        return table ;
    }

    /**
     * creates new table header element
     * @param targetParent
     * @param targetID
     * @param targetClass
     * @param targetTitle
     * @param targetText
     * @param appendParam
     * @returns created table header element
     * @private
     */
    function _createTableHeader(targetParent, targetID, targetClass, targetTitle, targetText, appendParam){
        var th = jQuery(document.createElement('th'));
        _assignBasicAttributes(th, targetID, targetClass, targetTitle);
        if(targetText)
            th.text(targetText);
        _assignElementToParent(th, targetParent, appendParam);
        return th ;
    }

    /**
     * creates new table row element
     * @param targetParent
     * @param targetID
     * @param targetClass
     * @param targetTitle
     * @param appendParam
     * @returns created table row element
     * @private
     */
    function _createTableRow(targetParent, targetID, targetClass, targetTitle, appendParam){
        var tr = jQuery(document.createElement('tr'));
        _assignBasicAttributes(tr, targetID, targetClass, targetTitle);
        _assignElementToParent(tr, targetParent, appendParam);
        return tr ;
    }

    /**
     * creates new table data element
     * @param targetParent
     * @param targetID
     * @param targetClass
     * @param targetTitle
     * @param targetText
     * @param appendParam
     * @returns created table data element
     * @private
     */
    function _createTableData(targetParent, targetID, targetClass, targetTitle, targetText, appendParam){
        var td = jQuery(document.createElement('td'));
        _assignBasicAttributes(td, targetID, targetClass, targetTitle);
        if(targetText)
            td.text(targetText);
        _assignElementToParent(td, targetParent, appendParam);
        return td ;
    }

    /**
     * creates a new icon element
     * @param targetParent
     * @param targetID
     * @param targetClass
     * @param targetTitle
     * @param appendParam
     * @returns created icon element
     * @private
     */
    function _createIcon(targetParent, targetID, targetClass, targetTitle, appendParam){
        var i = jQuery(document.createElement('i'));
        _assignBasicAttributes(i, targetID, targetClass, targetTitle);
        _assignElementToParent(i, targetParent, appendParam);
        return i ;
    }

    /**
     * creates new image element
     * @param targetParent
     * @param targetID
     * @param targetClass
     * @param targetTitle
     * @param targetSource
     * @param appendParam
     * @returns created image element
     * @private
     */
    function _createImage(targetParent, targetID, targetClass, targetTitle, targetSource, appendParam){
        var img = jQuery(document.createElement('img'));
        _assignBasicAttributes(img, targetID, targetClass, targetTitle);
        if(targetSource)
            img.attr('src', targetSource);
        _assignElementToParent(img, targetParent, appendParam);
        return img ;
    }

    /**
     * creates a new link element
     * @param targetParent
     * @param targetID
     * @param targetClass
     * @param targetTitle
     * @param targetHref
     * @param targetText
     * @param appendParam
     * @returns created link element
     * @private
     */
    function _createLink(targetParent, targetID, targetClass, targetTitle, targetHref, targetText, appendParam){
        var a = jQuery(document.createElement('a'));
        _assignBasicAttributes(a, targetID, targetClass, targetTitle);
        if(targetHref)
            a.attr('href', targetHref);
        if(targetText)
            a.text(targetText);
        _assignElementToParent(a, targetParent, appendParam);
        return a ;
    }

    /**
     * creates a new form element
     * @param targetParent
     * @param targetID
     * @param targetClass
     * @param targetMethod
     * @param targetAction
     * @param appendParam
     * @returns created form element
     * @private
     */
    function _createForm(targetParent, targetID, targetClass, targetMethod, targetAction, appendParam){
        var form = jQuery(document.createElement('form'));
        _assignBasicAttributes(form, targetID, targetClass, null);
        if(targetMethod)
            form.attr('method', targetMethod);
        if(targetAction)
            form.attr('action', targetAction);
        _assignElementToParent(form, targetParent, appendParam);
        return form ;
    }

    /**
     * creates a new text input field element
     * @param targetParent
     * @param targetID
     * @param targetClass
     * @param targetTitle
     * @param targetText
     * @param targetPlaceholder
     * @param appendParam
     * @returns created text input field element
     * @private
     */
    function _createTextInput(targetParent, targetID, targetClass, targetTitle, targetText, targetPlaceholder, appendParam){
        var input = jQuery(document.createElement('input'));
        _assignBasicAttributes(input, targetID, targetClass, targetTitle);
        input.attr('type', 'text');
        if(targetText)
            input.attr('value', targetText);
        if(targetPlaceholder)
            input.attr('placeholder', targetPlaceholder);
        _assignElementToParent(input, targetParent, appendParam);
        return input;
    }

    /**
     * creates a new password input field element
     * @param targetParent
     * @param targetID
     * @param targetClass
     * @param targetTitle
     * @param targetText
     * @param targetPlaceholder
     * @param appendParam
     * @returns created password input field element
     * @private
     */
    function _createPasswordInput(targetParent, targetID, targetClass, targetTitle, targetText, targetPlaceholder, appendParam){
        var input = jQuery(document.createElement('input'));
        _assignBasicAttributes(input, targetID, targetClass, targetTitle);
        input.attr('type', 'password');
        if(targetText)
            input.attr('value', targetText);
        if(targetPlaceholder)
            input.attr('placeholder', targetPlaceholder);
        _assignElementToParent(input, targetParent, appendParam);
        return input;
    }

    /**
     * creates a new submit button element
     * @param targetParent
     * @param targetID
     * @param targetClass
     * @param targetTitle
     * @param targetText
     * @param appendParam
     * @returns created submit button element
     * @private
     */
    function _createSubmitButton(targetParent, targetID, targetClass, targetTitle, targetText, appendParam){
        var input = jQuery(document.createElement('input'));
        _assignBasicAttributes(input, targetID, targetClass, targetTitle);
        input.attr('type', 'submit');
        if(targetText)
            input.attr('value', targetText);
        _assignElementToParent(input, targetParent, appendParam);
        return input;
    }

    /**
     * creates a new default button element
     * @param targetParent
     * @param targetID
     * @param targetClass
     * @param targetTitle
     * @param targetText
     * @param appendParam
     * @returns created submit button element
     * @private
     */
    function _createDefaultButton(targetParent, targetID, targetClass, targetTitle, targetText, appendParam){
        var input = jQuery(document.createElement('input'));
        _assignBasicAttributes(input, targetID, targetClass, targetTitle);
        input.attr('type', 'button');
        if(targetText)
            input.attr('value', targetText);
        _assignElementToParent(input, targetParent, appendParam);
        return input;
    }

    /**
     * creates a new heading element specified by targetHeadingType
     * @param targetParent
     * @param targetID
     * @param targetClass
     * @param targetTitle
     * @param targetText
     * @param targetHeadingType
     * @param appendParam
     * @returns created heading element
     * @private
     */
    function _createHeading(targetParent, targetID, targetClass, targetTitle, targetText, targetHeadingType, appendParam){
        if(!targetHeadingType)
            return ;
        var h = jQuery(document.createElement(targetHeadingType));
        if(targetText)
            h.text(targetText);
        _assignBasicAttributes(h, targetID, targetClass, targetTitle);
        _assignElementToParent(h, targetParent, appendParam);
        return h ;
    }

    /**
     * creates a new label element
     * @param targetParent
     * @param targetID
     * @param targetClass
     * @param targetTitle
     * @param targetText
     * @param appendParam
     * @returns created label element
     * @private
     */
    function _createLabel(targetParent, targetID, targetClass, targetTitle, targetText, appendParam){
        var label = jQuery(document.createElement('label'));
        if(targetText)
            label.text(targetText);
        _assignBasicAttributes(label, targetID, targetClass, targetTitle);
        _assignElementToParent(label, targetParent, appendParam);
        return label ;
    }

    /**
     *  public interface
     */
    return{
        createHeaderLink: _createHeaderLink,
        createPageTitle: _createPageTitle,
        createDiv: _createDiv,
        createParagraph: _createParagraph,
        createSpan: _createSpan,
        createTable: _createTable,
        createTableHeader: _createTableHeader,
        createTableRow: _createTableRow,
        createTableData: _createTableData,
        createIcon: _createIcon,
        createImage: _createImage,
        createLink: _createLink,
        createForm: _createForm,
        createTextInput: _createTextInput,
        createPasswordInput: _createPasswordInput,
        createSubmitButton: _createSubmitButton,
        createDefaultButton: _createDefaultButton,
        createHeading: _createHeading,
        createLabel: _createLabel
    }

});